

#include "trace.h"

#ifdef _DEBUG
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>

bool debugOn = false;
bool fileLog = true;
int debugLevel = TRACE_DEBUG;
int screanDebugLevel = TRACE_DEBUG;
bool debugAllModule = false;
bool withFileName = false;
char* debugModule = NULL;
char* noDebugModule = NULL;
bool debugAspect = false;
char* aspect = NULL;
bool debugAllFunc = false;
char* debugFunc = NULL;
char* noDebugFunc = NULL;
bool debug_init = false;


char* noPathFileName(char* file)
{
	char* tempName = file;
	char* noPathName = file;

	while((tempName = strstr(tempName,"/")))
	{
		noPathName = ++tempName;
	}

	return noPathName;
}

bool checkDebugLevel(int lvl)
{
	if( lvl <= debugLevel ) 
		return true;
	else
		return false;
}

bool checkScreenDebugLevel(int lvl)
{
	if( lvl <= screanDebugLevel ) 
		return true;
	else
		return false;
}

bool checkModule(const char* file)
{
	char* copy = strdup((char*)file);
	if(noDebugModule != NULL)
	{
		if(strstr(noDebugModule,copy))
		{
			free(copy);
			return false; 
		}
	}

	if(debugAllModule)
	{
		free(copy);
		return true;
	}

	if(debugModule == NULL)
	{
		free(copy);
		return false;
	}
	
	if(strstr(debugModule,copy))
	{
		free(copy);
		return true;
	}
	else
	{
		free(copy);
		return false; 
	}
}

bool checkAspect(char* asp)
{
	if(!debugAspect)
	{
		return true;
	}
	
	if(asp == 0)
	{
		return false;
	}
	
	char* copy = strdup((char*)asp);
	if(aspect != NULL)
	{
		if(strstr(aspect,copy))
		{ 
			free(copy);
			return true; 
		}
		else
		{
			free(copy);
			return false; 
		}
	}
	return false;
}

bool checkFunc(const char* function)
{
	char* copy = strdup((char*)function);
	if(noDebugFunc != NULL)
	{
		if(strstr(noDebugFunc,copy))
		{ 
			free(copy);
			return false; 
		}
	}

	if(debugAllFunc)
	{
		free(copy);
		return true;
	}

	if(debugFunc == NULL)
	{
		free(copy);
		return false;
	} 
	
	if(strstr(debugFunc,copy))
	{
		free(copy);
		return true;
	}
	else
	{
		free(copy);
		return false; 
	}
}

void LoadProfile()
{
	char rawBuf[1024];
	char buf[1024];
	char skip;
	char* value;
	char* oldBuf = rawBuf;
	char* newBuf = buf;
	char iniFileName[512];	
	sprintf(iniFileName, "/usr/local/include/infiniband/debug.conf");
	FILE* iniFile = fopen(iniFileName,"r");

	if(iniFile == NULL)
	{
		printf("Can't open `%s\' file.\n", iniFileName);
		perror("fopen failed");
		return;
	}

	while(!feof(iniFile))
	{
		oldBuf = rawBuf;
		
		fread(oldBuf++,sizeof(char),1,iniFile);

		while(*(oldBuf-1) != '\n' && !feof(iniFile))
		{
			if(*(oldBuf-1) == '\\')
			{
				oldBuf--;

				do
				{
					fread(&skip,sizeof(char),1,iniFile);
				} while(skip != '\n' && !feof(iniFile) );
			}

			fread(oldBuf++,sizeof(char),1,iniFile);
		}
		
		if(!feof(iniFile))
		{
			if(*(oldBuf-1) == '\n')
				*(oldBuf-1) = 0;
			else
				*oldBuf =0;
		}
		else
			*(oldBuf-1) = 0;

		oldBuf = rawBuf;
		newBuf = buf;

		while(*oldBuf)
		{
			if((*oldBuf) != ' ' && (*oldBuf) != '\t')
			{
				*newBuf = *oldBuf;
				newBuf ++;
				oldBuf ++;
			}
			else
				oldBuf ++;
		}

		*newBuf = 0;
		//printf("%s\n",buf);

		if(strncmp(buf,"DebugLevel",strlen("DebugLevel"))==0)
		{
			value = buf + strlen("DebugLevel") + 1;
			debugLevel = atoi(value);
		}
		else if(strncmp(buf,"FileLog",strlen("FileLog"))==0)
		{
			value = buf + strlen("FileLog") + 1;
			if(strcmp(value,"true") == 0)
				fileLog = true;
			else if(strcmp(value,"false") == 0)
				fileLog = false;
		}
		else if(strncmp(buf,"DebugOn",strlen("DebugOn"))==0)
		{
			value = buf + strlen("DebugOn") + 1;
			if(strcmp(value,"true") == 0)
				debugOn = true;
			else if(strcmp(value,"false") == 0)
				debugOn = false;
		}
		else if(strncmp(buf,"DebugAllModule",strlen("DebugAllModule"))==0)
		{
			value = buf + strlen("DebugAllModule") + 1;
			if(strcmp(value,"true") == 0)
				debugAllModule = true;
			else if(strcmp(value,"false") == 0)
				debugAllModule = false;
		}
		else if(strncmp(buf,"DebugAllFunction",strlen("DebugAllFunction"))==0)
		{
			value = buf + strlen("DebugAllFunction") + 1;
			if(strcmp(value,"true") == 0)
				debugAllFunc = true;
			else if(strcmp(value,"false") == 0)
				debugAllFunc = false;
		}
		else if(strncmp(buf,"WithFileName",strlen("WithFileName"))==0)
		{
			value = buf + strlen("WithFileName") + 1;
			if(strcmp(value,"true") == 0)
				withFileName = true;
			else if(strcmp(value,"false") == 0)
				withFileName = false;
		}
		else if(strncmp(buf,"DebugModule",strlen("DebugModule"))==0)
		{
			value = buf + strlen("DebugModule") + 1;
			debugModule = strdup(value);
		}
		else if(strncmp(buf,"NoDebugModule",strlen("NoDebugModule"))==0)
		{
			value = buf + strlen("NoDebugModule") + 1;
			noDebugModule = strdup(value);
		}
		else if(strncmp(buf,"DebugFunction",strlen("DebugFunction"))==0)
		{
			value = buf + strlen("DebugFunction") + 1;
			debugFunc = strdup(value);
		}
		else if(strncmp(buf,"NoDebugFunction",strlen("NoDebugFunction"))==0)
		{
			value = buf + strlen("NoDebugFunction") + 1;
			noDebugFunc = strdup(value);
		}
	}

	fclose(iniFile);
}

#endif
